package com.car.job;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.car.common.DefaultConfig;
import com.car.common.model.Card;
import com.car.common.model.CardDiscuss;
import com.car.common.model.TempWrap;
import com.car.common.model.UserSees;
import com.car.common.model.Users;
import com.car.tx.UserWrap;
import com.car.tx.UserWrap2;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.utils.DateUtil;
import com.utils.RongCloudUtil;
import com.utils.StringUtil;
import com.utils.SystemClock;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by chengjun on 2019/7/26.
 */
public class QuartzOnlineJob implements Job {
	
	private static Logger logger = Logger.getLogger(QuartzOnlineJob.class);
	
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    	logger.info("--------QuartzOnlineJob start "+ DateUtil.meetDateTimeFormat.format(new Date()) +"----------");
    	Db.update("update users set login_status = 1  where  login_status = 0 and logout_time < " + SystemClock.now()/1000L);
    	
    	List<Users> usersList = Users.dao.find("select id from users where is_true > 1 and login_status != 0 order by RAND() limit 150  ");
    	for(Users one : usersList) {
    		long time = (1+ new Random().nextInt(6)) *6*60L;
    		Db.update("update users set login_status = 0,logout_time = "+ (SystemClock.now()/1000L + time ) + "  where id = " + one.getId());
    	}
    	
    	String andSql = "select id from users as u where is_true = 1 and sex = 1 and login_status != 0 and up_time <  " + (SystemClock.now()/1000L - 6*30*24*3600L);
        List<Users> users = Users.dao.find(andSql + " order by rand() limit 500 ");
        for(Users one : users) {
        	long time = (1+ new Random().nextInt(6)) *6*60L;
        	Db.update("update users set login_status = 0,logout_time = "+ (SystemClock.now()/1000L + time ) +"  where id = " + one.getId());
        }
    	
    	
    	
    	// 随机评论
		List<CardDiscuss> discuss_list = CardDiscuss.dao.find("select discuss.id,discuss.user_id,discuss.card_id from card_discuss as discuss left join card  on discuss.card_id = card.id where discuss.card_id > 0 and card.create_time <  " + SystemClock.now()/1000L + " and discuss.create_time = " + SystemClock.FUTURE_TIME +" order by rand() limit 20 ");
		for(CardDiscuss discuss:discuss_list) {
			Db.update("update users set login_status = 0,logout_time = "+ (SystemClock.now()/1000L + 5 * 6 * 60L ) + "  where id = " + discuss.getUserId());
			
			Db.update("update card set discuss = discuss + 1,discuss_cnt = discuss_cnt + 1,views = views + 8,t_views = t_views + 8 where id = " + discuss.getCardId());
			Db.update("update card_discuss set create_time = " + (SystemClock.now()/1000L + new Random().nextInt(400) ) + " where id = " +  discuss.getId());
//    				System.out.println("update card set create_time = " + (SystemClock.now()/1000L + new Random().nextInt(300) ) + " where id = " +  card.getId());
		}
		
		// 审贴
		Calendar calendar = Calendar.getInstance();
    	int curHour24 = calendar.get(Calendar.HOUR_OF_DAY);
    	if(curHour24 < 4) {
    		Card new_card = Card.dao.findFirst("select id from card where is_show = 2 and is_check = 0 order by id asc limit 1 ");
    		if (new_card != null) {
    			Db.update("update card set is_show = 1,is_check = 1,views = views + 8,t_views = t_views + 8 where id = " + new_card.getId());
    		}
    	}
		logger.info("--------QuartzOnlineJob end "+ DateUtil.meetDateTimeFormat.format(new Date()) +"----------");
    	
    }
}
